package com.ctz.commonframe.ui.widget;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctz.commonframe.R;
import com.ctz.commonframe.entity.ImageItem;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.utils.GlideApp;


/**
 * 选择图片
 */
public class ImageChooseActivity extends TitleBarActivity {
    private List<ImageItem> imageList;
    private List<ImageItem> selectedImageList = new ArrayList<ImageItem>();
    private String mBucketName;
    private GridView mGridView;
    private ImageGridAdapter mAdapter;
    private Button mFinishBtn;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_choose);
        imageList = (List<ImageItem>) getIntent().getSerializableExtra(ImageBucketChooseActivity.EXTRA_IMAGE_LIST);
        if (imageList == null)
            imageList = new ArrayList<ImageItem>();
        mBucketName = getIntent().getStringExtra(ImageBucketChooseActivity.EXTRA_BUCKET_NAME);

        if (TextUtils.isEmpty(mBucketName)) {
            mBucketName = "选择图片";
        }
        initView();
        initListener();

    }

    private void initView() {
        setTitleMiddleText(mBucketName);
        mGridView = (GridView) findViewById(R.id.gridview);
        mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        mAdapter = new ImageGridAdapter(ImageChooseActivity.this, imageList);
        mGridView.setAdapter(mAdapter);
        mFinishBtn = (Button) findViewById(R.id.subTv);
    }

    private void initListener() {
        setTitleLeftClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });

        mFinishBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (selectedImageList.size() == 0) {
                    setResult(ImageBucketChooseActivity.RESULT_FINISH);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra(ImageBucketChooseActivity.KEY_SELECTED_IMAGES, (Serializable) selectedImageList);
                    setResult(RESULT_OK, intent);
                }

                finish();
            }
        });

        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ImageItem item = mAdapter.getItem(position);
                if (item.isSelected) {
                    item.isSelected = false;
                    selectedImageList.remove(item);
                } else {
                    item.isSelected = true;
                    selectedImageList.add(item);
                }
                mAdapter.notifyDataSetChanged();

                if (selectedImageList.size() == 0) {
                    mFinishBtn.setText("取消");
                } else {
                    mFinishBtn.setText("添加" + "(" + selectedImageList.size() + ")");
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }

    private class ImageGridAdapter extends AbsAdapter<ImageItem> {
        public ImageGridAdapter(Context context, List<ImageItem> dataList) {
            super(context, dataList, R.layout.item_image_choose_grid);
        }

        @Override
        public void convert(AdapterViewHolder holder, ImageItem data) {
            ImageView imageIv = holder.getView(R.id.image);
            TextView selectedBgTv = holder.getView(R.id.image_selected_bg);
            ImageView selectedIv = holder.getView(R.id.selected_tag);
            GlideApp.with(context).clear(imageIv);
            if(data.thumbnailPath != null){
                GlideApp.with(context).load(data.thumbnailPath).into(imageIv);
            }else if(data.imagePath != null){
                GlideApp.with(context).load(data.imagePath).into(imageIv);
            }
            if (data.isSelected) {
                selectedIv.setImageResource(R.drawable.select_tag);
                selectedIv.setVisibility(View.VISIBLE);
                selectedBgTv.setBackgroundResource(R.drawable.bg_image_selected);
            } else {
                selectedIv.setImageDrawable(null);
                selectedIv.setVisibility(View.GONE);
                selectedBgTv.setBackgroundResource(R.color.gray);
            }
        }
    }
}